package com.dsr.util;

import cn.hutool.core.date.DateTime;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

public class JwtUtil {

    private static final   String  SECRET ="11q2w3e4r%$24";

    public static String getToken(Map<String,String> map){
        JWTCreator.Builder builder = JWT.create();
        map.put("time", DateTime.now().toString());
        map.forEach(builder::withClaim);
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.HOUR,10);
        builder.withExpiresAt(instance.getTime());
        return  builder.sign(Algorithm.HMAC256(SECRET)).toString();
    }

    public static void verify(String token){
        JWT.require(Algorithm.HMAC256(SECRET)).build().verify(token);
    }
    public static DecodedJWT getDecodedJWT(String token){
        return JWT.require(Algorithm.HMAC256(SECRET)).build().verify(token);
    }
}
